window.tabs=window.tabs || [];
window.activeTab=typeof(window.activeTab)=="number"?window.activeTab:-1;
window.hoveringTab=-1;
window.draggingTab=-1;

window.addEventListener("mouseup",function(evt){
	window.draggingTab=-1;
},false);

function addTab(url,noFocus){try{
	if(!url)
		url="about:blank";
	
	var id=tabs.length;
	
	var tab=document.createElementNS(XUL,"browser");
	tab.setAttribute("type","content-primary");
	tab.onerror=function(errorMsg, url, lineNumber){
		window.Notify.echo("Error in Content: \n"+errorMsg+" @"+(url||"<unknown>")+":"+lineNumber,1);
	};
	tab.setAttribute("flex","1");
	tab.setAttribute("class","tab inactive");
	
	tab.onclick=function(evt){
		if(evt.button==1 && evt.target && evt.target.href){
			addTab(evt.target.href);
			evt.stop();
		}else if(evt.button==2){
			historyBack();
			evt.stop();
		}
	};
	tab.onload=null;

	var tabButton=document.createElement("div");
	tabButton.setAttribute("class","button inactive");
	
	tabButton.onmousedown=function(evt){
		if(activeTab!=this.tabOffset)
			showTab(this.tabOffset);
		window.draggingTab=this.tabOffset;
		
		evt.stop();
	};
	
	tabButton.onmouseup=function(evt){
		
	};
	
	tabButton.onmousemove=function(evt){
		window.hoveringTab=this.tabOffset;
		if(window.draggingTab!=(-1) && window.draggingTab!=window.hoveringTab){
			moveActiveTab(window.hoveringTab);
			window.draggingTab=window.activeTab;
		}else if(window.draggingTab==(-1))
			previewTab(this.tabOffset);
	};
	
	tabButton.onmouseout=function(evt){
		closePreviewTab(this.tabOffset);
	};
	
	var tabIcon=document.createElement("img");
	tabButton.appendChild(tabIcon);
	
	var tabText=document.createElement("span");
	tabButton.appendChild(tabText);
	
	var tabClose=document.createElement("div");
	var tabCloseIcon=document.createElement("img");
	tabCloseIcon.src="style/button_close.png";
	tabClose.appendChild(tabCloseIcon);
	tabClose.className=("closebutton");
	tabButton.appendChild(tabClose);
	tabClose.onclick=(function(evt){
		closeTab(this.tabOffset);
		evt.stop();
	}).bind(tabButton);
	
	document.querySelector("#browsers").appendChild(tab);
	document.querySelector("#tab-buttons").appendChild(tabButton);
	window.setTimeout(function(){
		if(!tab.webNavigation.sessionHistory)
			tab.webNavigation.sessionHistory=Components.classes["@mozilla.org/browser/shistory;1"].createInstance(Components.interfaces.nsISHistory);
	},0);
	
	tab.setAttribute("src",url);
	
	tabs.push({
		"tab":tab,
		"button":tabButton,
		"lastTitle":"",
		"lastDomain":"",
		"lastIconUrl":"",
		"history":[],
		"historyOffset":-1,
		"preview":document.createElement("canvas")
	});

	for(var i=0;i<tabs.length;i++){
		tabs[i].tab.tabOffset=i;
		tabs[i].button.tabOffset=i;
	}
						
	if(!noFocus)
		showTab(id);
}catch(e){trace(e);}}

function closeTab(offset){try{
	if(!tabs[offset] || tabs.length<=1)
		return;
	
	var nextTab;
	if(offset==activeTab){
		if(offset!=tabs.length-1){
			showTab(offset+1);
			nextTab=offset;
		}else{
			showTab(offset-1)
			nextTab=offset-1;
		}
	}else{
		if(offset<activeTab){
			nextTab=activeTab-1;
		}else{
			nextTab=activeTab;
		}
	}
	
	var t=tabs[offset];
	
	t.tab.parentNode.removeChild(t.tab);
	t.button.parentNode.removeChild(t.button);
	
	tabs.splice(offset,1);
	
	activeTab=nextTab;
	
	for(var i=0;i<tabs.length;i++){
		tabs[i].tab.tabOffset=i;
		tabs[i].button.tabOffset=i;
	}
}catch(e){trace(e);}}

function moveActiveTab(offset){try{
	if(tabs.length<=1 || offset==activeTab)
		return;
	
	var activeTabObj=tabs[activeTab];

	
	if(offset>=tabs.length)
		offset=tabs.length-1;
	
	var targetTabObj=tabs[offset];

	if(activeTab<offset){
		targetTabObj.button.parentNode.insertAfter(activeTabObj.button,targetTabObj.button);
		tabs.splice(offset+1,0,activeTabObj);
		tabs.splice(activeTab,1);
	}else{
		targetTabObj.button.parentNode.insertBefore(activeTabObj.button,targetTabObj.button);
		tabs.splice(activeTab,1);
		tabs.splice(offset,0,activeTabObj);
	}
	
	activeTab=offset;
	
	for(var i=0;i<tabs.length;i++){
		tabs[i].tab.tabOffset=i;
		tabs[i].button.tabOffset=i;
	}
}catch(e){trace(e);}}


function showTab(offset){try{
	if(typeof(offset)=="object")
		offset=offset.tabOffset;
		
	var o=document.querySelector("#overlay");
	if(tabs[activeTab]){
		tabs[activeTab].preview.width=o.width;
		tabs[activeTab].preview.height=o.height;
		tabs[activeTab].preview.getContext("2d").drawWindow(tabs[activeTab].tab.contentWindow,0,0,o.width,o.height,"#ffffff");
	}
	
	activeTab=offset;
	
	for(var i=0;i<tabs.length;i++){
		if(i==offset){
			tabs[i].tab.setAttribute("class","tab active");
			tabs[i].button.setAttribute("class","button active");
			tabs[i].tab.addEventListener("DOMContentLoaded",onLoad,false);
		}else{
			tabs[i].tab.setAttribute("class","tab inactive");
			tabs[i].button.setAttribute("class","button inactive");
			
			tabs[i].tab.removeEventListener("DOMContentLoaded",onLoad,false);
		}
	}
	document.querySelector("#title").textContent=tabs[activeTab].lastTitle;
	document.querySelector("#domain").textContent=tabs[activeTab].lastDomain;
}catch(e){trace(e);}}

function showTitles(){try{
	
	for(var i=0;i<tabs.length;i++){
		var label=tabs[i].button.querySelector("span");
		var icon=tabs[i].button.querySelector("img");
		var domainLabel=document.querySelector("#domain");
		var doc=tabs[i].tab.contentWindow.document;
		if(doc.title!=tabs[i].lastTitle){
			label.textContent=tabs[i].lastTitle=doc.title;
			if(i==activeTab){
				document.querySelector("#title").textContent=tabs[i].lastTitle;
			}
			
		}
		
		var domain=tabs[i].tab.contentWindow.location.host;
		
		if(domain && domain!=tabs[i].lastDomain){
			tabs[i].lastDomain=domain;
			if(i==activeTab)
				domainLabel.textContent=tabs[i].lastDomain;
		}
		
		
		
		if(domain){
			var iconUrl=tabs[i].tab.contentWindow.location.protocol+"//"+domain+"/favicon.ico";
			if(iconUrl!=tabs[i].lastIconUrl)
				icon.setAttribute("src",tabs[i].lastIconUrl=iconUrl);
		}
	}

}catch(e){trace(e);}}